package com.kiln.juxingqs.fragment

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.base.BaseFragment
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.DddjyAdapter
import com.kiln.juxingqs.api.bean.GetDddRet
import com.kiln.juxingqs.databinding.FragmentWdjponeBinding
import com.kiln.juxingqs.dialog.BathDialog
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.ui.vm.DddjyViewModel

class DddjyOneFragment : BaseFragment<FragmentWdjponeBinding, DddjyViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_wdjpone
    }

    val adapter: DddjyAdapter by lazy {
        DddjyAdapter(requireContext()).apply {
            sellFun = {

                JyInputNumDialog(requireContext()).apply {
                    ok = { num ->
                        if (it.num < num.toInt()) {
                            toast("数量不能大于可买数量")
                        } else {
                            viewModel.sellNum.value = "${num}"
                            viewModel.currOrder.value = it
                            holdBath()
                            dismiss()
                        }
                    }
                }.show()


            }
        }
    }


    override fun initViewModel(): DddjyViewModel = createViewModel()

    override fun initWidget() {

        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        val decoration = DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)

        binding.recyclerView.adapter = adapter
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdOne()
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true

            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.order_list as MutableList<GetDddRet.Order?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
        viewModel.sellRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("成功")
                    viewModel.pageOne.value = 0
                    viewModel.holdOne()
                }
            }
        })
    }

    //挂卖获取批次
    fun holdBath() {
        BathDialog(requireActivity()).apply {
            vm = viewModel
            ok = {
                viewModel.holdSell(it)
            }
        }.show()
    }

    override fun initData() {
        viewModel.pageOne.value = 0
        viewModel.holdOne()
    }

}